package lt.egidijus.aatest.genetic;
import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class CoverageParser {
	
	private CoverageClass covClass = null;
	
	public CoverageParser(CoverageClass cov) {
		covClass = cov;
	}
	
	
	
	public void parsingXml(File xml){
		 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = null;
		try {
		  docBuilder = docBuilderFactory.newDocumentBuilder();
         Document doc = docBuilder.parse (xml);
         doc.getDocumentElement ().normalize ();
         NodeList listOfNodes =  doc.getChildNodes();
         iterateParsing(listOfNodes);
	} catch (SAXException e) {
		e.printStackTrace();
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ParserConfigurationException e) {
		e.printStackTrace();
	}
	}
	
	private void parsingClass(NodeList packageNode){
		for (int i = 0; i < packageNode.getLength(); i++){
			NodeList childNodes = packageNode.item(i).getChildNodes();
			for (int j = 0; j < childNodes.getLength(); j++ ){
			  	if (childNodes.item(j).getNodeName().equals("class") &&
			  			childNodes.item(j).getAttributes().getNamedItem("name").getNodeValue().equals(covClass.getClassName())){
			  		parsingMethods(childNodes.item(j).getChildNodes());
	
			  	}
			}
		}
		
		
	}
	
	public void parsingMethods(NodeList classNodes){
  		for (int j = 0;  j< classNodes.getLength(); j++){
  			Node n = classNodes.item(j);
  			if (n.getNodeName().equals("coverage") && 
  					n.getAttributes().getNamedItem("type").getNodeValue().contains("line")){
  				String classCoverage = n.getAttributes().getNamedItem("value").getNodeValue();	
  				covClass.setClassCoverageValue(classCoverage);
  			}
  			if (n.getNodeName().equals("method")){
  				String methodName = n.getAttributes().getNamedItem("name").getNodeValue();	
  				NodeList methodCoverages = n.getChildNodes();
  				String methodCoverage = "";
  				for (int k = 0;  k< methodCoverages.getLength(); k++){
  					if (methodCoverages.item(k).getNodeName().equals("coverage") && 
  							methodCoverages.item(k).getAttributes().getNamedItem("type").getNodeValue().contains("line")){
  						methodCoverage = methodCoverages.item(k).getAttributes().getNamedItem("value").getNodeValue();	
  					}			
  				}
  				if (!methodName.isEmpty() && !methodCoverage.isEmpty())
  					covClass.addMethodCoverage(methodName, methodCoverage);
  			}
  			
  		}
	}

	private void iterateParsing(NodeList nodelist)
	{
	    for(int i=0; i<nodelist.getLength(); i++){
	      Node childNode = nodelist.item(i);
	      if(childNode.getNodeType() == Node.ELEMENT_NODE){
	    	  	if (childNode.getNodeName().equals("all")){
	    	  		 NodeList children = childNode.getChildNodes();
	    	  		 for(int j=0; j<children.getLength(); j++){
	    	  			if (children.item(j).getNodeName().equals("coverage") && 
	    	  					children.item(j).getAttributes().getNamedItem("type").getNodeValue().contains("line")){
	    	  				NamedNodeMap map = children.item(j).getAttributes();
	    	  				String allCoverage = map.getNamedItem("value").getNodeValue();
	    	  				covClass.setAllCoverageValue(allCoverage);

	    	  			}
	    	  			if (children.item(j).getNodeName().equals("package") && 
	    	  					children.item(j).getAttributes().getNamedItem("name").getNodeValue().equals(covClass.getPackageName())){
	    	  				parsingClass(children.item(j).getChildNodes());
	    	  				return;
	    	  			}
	    	  		 }
	    	  	}	      
	    	  	
	      }

	      NodeList children = childNode.getChildNodes();
	      if (children != null)
	      {
	    	  iterateParsing(children);
	      }
	    }
	}
}
